﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.IO;
using System.Collections;

/*
 * Copyright (c) 2007-2012, Thomas Ardal (thomasardalatgmail.com), Lex Li (supportatlextm.com), Rami Abughazaleh (rami.abughazalehatgmail.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of Thomas Ardal nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
namespace PerformanceDude.MSBuildShellExtension
{
    public class Solution
    {
        //internal class SolutionParser
        //Name: Microsoft.Build.Construction.SolutionParser
        //Assembly: Microsoft.Build, Version=4.0.0.0

        static readonly Type _SolutionParser;

        static readonly PropertyInfo _solutionReader;
        static readonly PropertyInfo _projects;
        static readonly PropertyInfo _SolutionFile;
        static readonly PropertyInfo _SolutionConfigurations;
        static readonly PropertyInfo _Version;

        static readonly MethodInfo _parseSolution;
        static readonly MethodInfo _parseSolutionFileForConversion;
        static readonly MethodInfo _ParseSolutionConfigurations;

        static Solution()
        {
            _SolutionParser = Type.GetType("Microsoft.Build.Construction.SolutionParser, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
            if (_SolutionParser != null)
            {
                _solutionReader = _SolutionParser.GetProperty("SolutionReader", BindingFlags.NonPublic | BindingFlags.Instance);
                _projects = _SolutionParser.GetProperty("Projects", BindingFlags.NonPublic | BindingFlags.Instance);
                _parseSolution = _SolutionParser.GetMethod("ParseSolution", BindingFlags.NonPublic | BindingFlags.Instance);
                _parseSolutionFileForConversion = _SolutionParser.GetMethod("ParseSolutionFileForConversion", BindingFlags.NonPublic | BindingFlags.Instance);
                _ParseSolutionConfigurations = _SolutionParser.GetMethod("ParseSolutionConfigurations", BindingFlags.NonPublic | BindingFlags.Instance);
                
                _projects = _SolutionParser.GetProperty("Projects", BindingFlags.NonPublic | BindingFlags.Instance);
                _SolutionFile = _SolutionParser.GetProperty("SolutionFile", BindingFlags.NonPublic | BindingFlags.Instance);
                _SolutionConfigurations = _SolutionParser.GetProperty("SolutionConfigurations", BindingFlags.NonPublic | BindingFlags.Instance);
                _Version = _SolutionParser.GetProperty("Version", BindingFlags.NonPublic | BindingFlags.Instance);
            }
        }

        public List<SolutionProject> Projects { get; private set; }
        public List<SolutionConfiguration> SolutionConfigurations { get; private set; }
        public int Version { get; private set; }

        public Solution(string solutionFileName)
        {
            if (_SolutionParser == null)
            {
                throw new InvalidOperationException("Can not find type 'Microsoft.Build.Construction.SolutionParser' are you missing a assembly reference to 'Microsoft.Build.dll'?");
            }

            var solutionParser = _SolutionParser.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First().Invoke(null);
            using (var streamReader = new StreamReader(solutionFileName))
            {
                _solutionReader.SetValue(solutionParser, streamReader, null);
                //ParseSolution method does not support Visual Studio 2008
                //_parseSolution.Invoke(solutionParser, null);
                _SolutionFile.SetValue(solutionParser, solutionFileName, null);
                _parseSolutionFileForConversion.Invoke(solutionParser, null);
                //ParseSolutionConfigurations requires ParseSolution
                //_ParseSolutionConfigurations.Invoke(solutionParser, null);
            }

            var projects = new List<SolutionProject>();
            var array = (Array)_projects.GetValue(solutionParser, null);
            for (int i = 0; i < array.Length; i++)
            {
                projects.Add(new SolutionProject(array.GetValue(i)));
            }
            this.Projects = projects;

            var solutionConfigurations = new List<SolutionConfiguration>();
            var solutionConfigurationsArray = (IList)_SolutionConfigurations.GetValue(solutionParser, null);
            for (int i = 0; i < solutionConfigurationsArray.Count; i++)
            {
                solutionConfigurations.Add(new SolutionConfiguration(solutionConfigurationsArray[i]));
            }
            this.SolutionConfigurations = solutionConfigurations;

            this.Version = (int)_Version.GetValue(solutionParser, null);
        }
    }
}
